package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kittlen.managementSystem.entity.Admin;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Result;
import com.kittlen.managementSystem.entity.VChoice;
import com.kittlen.managementSystem.service.IVChoiceService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学员选择(学员主) 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-01-30
 */
@RestController
@RequestMapping("/vChoice")
public class VChoiceController {

    @Resource
    IVChoiceService ivChoiceService;

    //查询学员的信息(包含教练信息)
    @RequestMapping("/selects")
    public Result selects(VChoice vChoice, @RequestParam(required = false, defaultValue = "0") int page,
                              @RequestParam(required = false, defaultValue = "0") int limit){
        Result result=new Result();
        QueryWrapper<VChoice> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNoEmpty(vChoice.getStudentId())){
            queryWrapper.like("student_id", vChoice.getStudentId());
        }
        if(ObjectUtil.isNoEmpty(vChoice.getFullname())){
            queryWrapper.like("fullname", vChoice.getFullname());
        }
        if(ObjectUtil.isNoEmpty(vChoice.getPhone())){
            queryWrapper.eq("phone", vChoice.getPhone());
        }
        //是否毕业
        if(ObjectUtil.isNoEmpty(vChoice.getIsEnd())){
            queryWrapper.eq("is_end", vChoice.getIsEnd());
        }
        //当前学习阶段
        if(ObjectUtil.isNoEmpty(vChoice.getStudyStatus())){
            queryWrapper.eq("study_status", vChoice.getStudyStatus());
        }
        //当前学习阶段是否结束
        if(ObjectUtil.isNoEmpty(vChoice.getStudyType())){
            queryWrapper.eq("study_type", vChoice.getStudyType());
        }
        //教练id
        if(ObjectUtil.isNoEmpty(vChoice.getCoachId())){
            queryWrapper.eq("coach_id", vChoice.getCoachId());
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        List<VChoice> list=ivChoiceService.list(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(ivChoiceService.count(queryWrapper));
        return result;
    }
}
